*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	This program estimates OLS VAM
*	----------------------------------------------------------------------------

	args sample sch_res vtype bw

	if "${city}"=="DEN" local ptypes form
	if "${city}"=="NYC" local ptypes form2
	if "${city}"=="NYCms" local ptypes form2

	local bins 20

	local het 1

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*	load analysis file

	use "${builddata}${city}_analysisfile`sample'`sch_res'`bw'", clear

	cap drop __*
	cap drop offersum
	egen offersum = rowmax(offer_*)

	foreach y in m e {
		ivset, y(`y')
			local Y=r(Y)
			local D=r(D)

	*	conventional in the full sample
		//uncontrolled

		olsvam `Y' `D', 				gen(OLSVAM_`y'_func) covs(i.year##i.grade) vce(`vtype')
		schvam OLSVAM_`y'_func, 		d(`D') gen(b_OLSVAM_`y'_func)
		//lagged score
		olsvam `Y' `D', 				gen(OLSVAM_`y'_flag) covs(i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
		schvam OLSVAM_`y'_flag,			d(`D') gen(b_OLSVAM_`y'_flag)

		if "`y'" == "m" & `het' {

			local D_bl_year
			local D_fr_lunch
			local D_sped
			local D_scores
			xtile terciles = bl_math, n(3)
			qui levelsof year, local(years)
			foreach var of varlist `D' {

				foreach yr in `years' {
					g `var'_`yr' = `var' * (year==`yr')
					local D_bl_year `D_bl_year' `var'_`yr'
				}

				g `var'_fr = `var' * bl_fr_lunch
				g `var'_no_fr = `var' * !bl_fr_lunch
				local D_fr_lunch `D_fr_lunch' `var'_fr `var'_no_fr

				g `var'_sp = `var' * bl_sped
				g `var'_no_sp = `var' * !bl_sped
				local D_sped `D_sped' `var'_sp `var'_no_sp

				g `var'_ter1 = `var' * (terciles==1)
				g `var'_ter2 = `var' * (terciles==2)
				g `var'_ter3 = `var' * (terciles==3)
				local D_scores `D_scores' `var'_ter1 `var'_ter2 `var'_ter3

	 		}

			//lagged score by year
			olsvam `Y' `D_bl_year', 		gen(OLSVAM_`y'_flag_yr) covs(i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_fr_lunch', 		gen(OLSVAM_`y'_flag_fr) covs(i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_sped', 			gen(OLSVAM_`y'_flag_sp) covs(i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_scores', 			gen(OLSVAM_`y'_flag_sc) covs(i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')

		}

		foreach ptype in `ptypes'{

			ivset, y(`y') ptype(`ptype')
				local p=r(P)
				local S=r(S)

		if "`y'" == "m" & `het' {

			//bin to get binned lottery sample
			ivset, ptype(`ptype')
				local S=r(S)
				vambin if `S', n(`bins') ptype(`ptype') vam(b_OLSVAM_m_flag)
			ivset, ptype(`ptype') bin
				local S=r(S)

		*	conventional in the risk sample

			//uncontrolled
			olsvam `Y' `D' if `S', 				gen(OLSVAM_`y'_`ptype'_unc) vce(`vtype') covs(i.year##i.grade)
			schvam OLSVAM_`y'_`ptype'_unc, 		d(`D') gen(b_OLSVAM_`y'_`ptype'_unc)
			//lagged score
			olsvam `Y' `D' if `S', 				gen(OLSVAM_`y'_`ptype'_lag) covs(i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			schvam OLSVAM_`y'_`ptype'_lag, 		d(`D') gen(b_OLSVAM_`y'_`ptype'_lag)

		*	p-score controlled in the risk sample

			//uncontrolled
			olsvam `Y' `D' if `S', 					gen(POLSVAM_`y'_`ptype'_unc) covs(`p' i.year##i.grade) vce(`vtype')
			schvam POLSVAM_`y'_`ptype'_unc, 		d(`D') gen(b_POLSVAM_`y'_`ptype'_unc)
			//lagged score
			olsvam `Y' `D' if `S', 					gen(POLSVAM_`y'_`ptype'_lag) covs(`p' i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			schvam POLSVAM_`y'_`ptype'_lag, 		d(`D') gen(b_POLSVAM_`y'_`ptype'_lag)

		* 	covariates without pscores in risk sample

			//lagged score by FR lunch
			olsvam `Y' `D_bl_year' if `S', 			gen(OLSVAM_`y'_lag_yr) covs(i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_fr_lunch' if `S', 		gen(OLSVAM_`y'_lag_fr) covs(i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_sped' if `S', 			gen(OLSVAM_`y'_lag_sp) covs(i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_scores' if `S', 			gen(OLSVAM_`y'_lag_sc) covs(i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')

		* 	covariates with pscores in risk sample

			//lagged score by FR lunch
			olsvam `Y' `D_bl_year' if `S', 			gen(POLSVAM_`y'_lag_yr) covs(`p' i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_fr_lunch' if `S', 		gen(POLSVAM_`y'_lag_fr) covs(`p' i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_sped' if `S', 			gen(POLSVAM_`y'_lag_sp) covs(`p' i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_scores' if `S', 			gen(POLSVAM_`y'_lag_sc) covs(`p' i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')

		* 	covariates with pscores in full sample

			//lagged score by FR lunch
			olsvam `Y' `D_bl_year', 			gen(POLSVAM_`y'_flag_yr) covs(`p' i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_fr_lunch', 			gen(POLSVAM_`y'_flag_fr) covs(`p' i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_sped', 				gen(POLSVAM_`y'_flag_sp) covs(`p' i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			//lagged score by FR lunch
			olsvam `Y' `D_scores', 				gen(POLSVAM_`y'_flag_sc) covs(`p' i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')

		}

		*	p-score controlled in the full sample

			//uncontrolled
			olsvam `Y' `D', 					gen(POLSVAM_`y'_`ptype'_func) covs(`p' i.year##i.grade) vce(`vtype')
			schvam POLSVAM_`y'_`ptype'_func, 	d(`D') gen(b_POLSVAM_`y'_`ptype'_func)
			//lagged score
			olsvam `Y' `D', 					gen(POLSVAM_`y'_`ptype'_flag) covs(`p' i.year##(i.grade c.($bl_demos) c.($bl_scores))) vce(`vtype')
			schvam POLSVAM_`y'_`ptype'_flag, 	d(`D') gen(b_POLSVAM_`y'_`ptype'_flag)

		}

	}

	*	save
	qui compress
	save "${builddata}${city}_analysisfile_vam`sample'`sch_res'`bw'", replace
